<!DOCTYPE html>
<html>
    <head>
        <title>iMehfile - News</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="../styles/style_news.css" rel="stylesheet" type="text/css" />
	<link href="../styles/cadre_info.css" rel="stylesheet" type="text/css" />
	<link href="../styles/main.css" rel="stylesheet" type="text/css" />
    </head>
    <body>

<?php

    $delete = false;
    $target_path = "../images/";
    if(isset($_POST['image_delete_button']))
    {
	$delete = true;
	delete_item($_POST['image_delete_button']);
    }
    if($delete==false)
    {
	if(isset($_POST['dateinput'],$_POST['titleinput'],$_POST['contentinput'],$_POST['imageinput']))
	{
	    if (strlen($_POST['dateinput'])+strlen($_POST['titleinput'])+strlen($_POST['contentinput'])+strlen($_POST['imageinput'])!=0) {
		add_item($_POST['dateinput'],$_POST['titleinput'],$_POST['contentinput'],$_POST['imageinput']);
	    }
	}
    }
    if($delete==false)
    {
	if(isset($_FILES['uploadedfile']))
	{
	    upload_file($_FILES['uploadedfile'],$target_path);
	}
    }

?>

    	<form enctype="multipart/form-data" method="post" action="newsmanager.php">
    		<p>
		    	<?php
					//$base = new SimpleXMLElement(get_xml_file(),null,true);
					$base = get_xml_file(false);
				?>
				<ul>
				    <li><a class="first" href="../index.php">Accueil</a></li>
				</ul>
				<table id="mytable" cellspacing="0" summary="Mehfile Zainab News">
				<caption>Mehfile Zainab News</caption>
				<tr>
				    <th scope="col" abbr="Configurations">Date</th>
				    <th scope="col" abbr="Dual 1.8">Titre</th>
				    <th scope="col" abbr="Dual 2">Description</th>
				    <th scope="col" abbr="Dual 2.5">Image</th>
				    <th scope="col" abbr="Dual 2.5">Action</th>
				</tr>
		    	<?php
					foreach($base->news->current_news as $current_news) {
				?>
				  <tr>
				    <th scope="row" abbr="Model" class="spec"><?php echo ($current_news->date) ?></th>
				    <td><?php echo ($current_news->title) ?></td>
				    <td><?php echo ($current_news->content) ?></td>
				    <td><?php echo '<img src="' . ($current_news->image) . '" align="center" width=50 height=50/>' ?></td>
				    <td><?php echo '<input type="image" src="icons/delete.png" alt="Supprimer" name="image_delete_button" value="' . ($current_news->title) . '" width=50 height=50/>' ?></td>
				  </tr>
		    	<?php
					}
				?>
				  <tr>
				    <th scope="row" abbr="Model" class="spec"><input type="text" name="dateinput"></th>
				    <td><input type="text" name="titleinput"></td>
				    <td><textarea rows="3" name="contentinput"></textarea></td>
				    <td>
				    <?php
				    	if((isset($_FILES['uploadedfile'])) && (strlen($_FILES['uploadedfile']['name'])!=0)) 
				    	{
					    		$image_location = $target_path . $_FILES['uploadedfile']['name'];
					    		$image_url = currentPageURL() . '/' . $image_location;
					    		echo '<input type="hidden" name="imageinput" value="' . $image_url . '" />';
							echo '<img src="' . $image_location . '" align="center" width=50 height=50/>';
				    	}
				    	else
				    	{
					?>
							<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
							<input name="uploadedfile" type="file" /><br />
							<input type="submit" value="Charger le fichier" />
					<?php
				    	}
				    ?>
				    </td>
				    <td><?php echo '<input type="image" src="icons/add.png" alt="Ajouter" name="image_add_button" width=50 height=50/>' ?></td>
				  </tr>
				</table>
	    	</p>
		</form>
		<div id="gauche">
		    <div id="droite">
			<div id="haut">
			<div></div>
			</div>
			<h2>Important : Procédure à suivre</h2>
			<p>
			    1. Choisir en tout premier l'image à uploader<br>
			    2. Charger/Uploader le fichier<br>
			    3. Remplir les autres champs de texte une fois que l'image ait été chargée<br>
			    4. Cliquer sur le bouton d'ajout du nouvel élément (en forme de +)<br>
			</p>
		    </div>
		</div>
    </body>
</html>
<?php

	function get_xml_file($onlyname)
	{
		$xmlfile = '../mzupdate.xml';
		if ($onlyname) 
		{
			return $xmlfile;	
		}
		return new SimpleXMLElement($xmlfile,null,true);
	}

	function delete_item($news_title) {
	    $item_to_delete = stripslashes($news_title);
	    //echo 'Delete item : ' . $item_to_delete . '<br>';
	    $base = get_xml_file(false);
	    $ref = 0;
		foreach($base->news->current_news as $current_news) {
		    $current_title = ($current_news->title);
		    //echo 'Compare to item : ' . $current_title . '<br>';
			if($current_title==$item_to_delete) {
			    unset($base->news->current_news[$ref]);
			    break;
			}
			$ref = $ref + 1;
		}
		$base->asXML(get_xml_file(true));
	}
	
	function add_item($date, $title, $content, $image) {
	    $base = get_xml_file(false);
	    $current_news = $base->news->addChild('current_news');
	    $current_news->addChild('title',stripslashes($title));
	    $current_news->addChild('content',stripslashes($content));
	    $current_news->addChild('date',stripslashes($date));
	    $current_news->addChild('image',($image));
	    $base->asXML(get_xml_file(true));
	    /*
	    $dom=new DomDocument();
	    $dom->load('mzupdate.xml');
	    //$dom->formatOutput = true;
	    $newsnode = $dom->getElementsByTagName("news")->item(0);
	    $currentNewsElem  = $dom->createElement('current_news'); 
	    $currentNewsElem->appendChild ( $dom->createElement('title', stripslashes($title)) );
	    $currentNewsElem->appendChild ( $dom->createElement('content',stripslashes($content)) );
	    $currentNewsElem->appendChild ( $dom->createElement('date',stripslashes($date)) );
	    $currentNewsElem->appendChild ( $dom->createElement('image',($image)) );
	    $newsnode->parentNode->insertBefore($currentNewsElem, $newsnode) ;
	    $dom->saveXML();
	    */
	}

	function upload_file($file,$target_path) {
		$target_dir = $target_path . basename( $file['name']);
		if(move_uploaded_file($file['tmp_name'], $target_dir)) 
		{} 
		else 
		{
		    echo "Erreur lors du chargement, contactez l'administrateur du site.<br>";
		}
	}
	
	function currentPageURL() {
		$pageURL = 'http';
		if($_SERVER["HTTPS"] == "on") 
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if($_SERVER["SERVER_PORT"] != "80") 
		{
			$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . dirname($_SERVER["REQUEST_URI"]);
		} 
		else 
		{
			$pageURL .= $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"]);
		}
		return $pageURL;
	}
	
?>